package com.hblack.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hblack.common.bean.EasyUIResult;
import com.hblack.manage.pojo.ItemParam;
import com.hblack.manage.service.ItemParamService;

@Controller
@RequestMapping(value="item/param")
public class ItemParamController {
	
	@Autowired
	private ItemParamService itemParamService;

	/**
	 * 根据cid查询商品类目的规格
	 * @param cid
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value="{cid}")
	public ResponseEntity<ItemParam> queryItemParamByCid(@PathVariable("cid") Long cid) {
		
		try {
			ItemParam itemParam = new ItemParam();
			itemParam.setItemCatId(cid);
			//根据CID查询商品规格
			ItemParam queryOne = this.itemParamService.queryOne(itemParam);
			
			if(queryOne == null) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}else {
				return ResponseEntity.ok(queryOne);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	
	/**
	 * 保存商品类目规格
	 * @param paramData
	 * @param cid
	 * @return
	 */
	@RequestMapping(value="{cid}",method=RequestMethod.POST)
	public ResponseEntity<Void> saveItemParam(@RequestParam("paramData") String paramData, @PathVariable("cid") Long cid){
		
		try {
			ItemParam itemParam = new ItemParam();
			itemParam.setItemCatId(cid);
			itemParam.setParamData(paramData);
			itemParam.setId(null);
			
			Integer itemParamCount = this.itemParamService.save(itemParam);
			
			if(itemParamCount == 1) {
				return ResponseEntity.status(HttpStatus.OK).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		
	}
	
	@RequestMapping(value="list",method=RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryItemParamByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum , @RequestParam(value="rows") Integer pageSize){
		
		try {
			EasyUIResult result = this.itemParamService.queryListByPage(pageNum,pageSize);
			
			if(result == null){
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			} else {
				return ResponseEntity.ok(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		
	}
}

























